package com.ruoyi.storeIndex.service.impl;

import com.ruoyi.storeIndex.domain.Event;
import com.ruoyi.storeIndex.domain.Publish;
import com.ruoyi.storeIndex.mapper.StoreStatisticsMapper;
import com.ruoyi.storeIndex.service.IStoreStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class StoreStatisticsServiceImpl implements IStoreStatisticsService {
    @Autowired
    private StoreStatisticsMapper statisticsMapper;
    @Override
    public Integer getPendingAuthNum(Long deptId) {
        return statisticsMapper.getPendingAuthNum(deptId);
    }

    @Override
    public Integer getSurveyNum(Long deptId) {
        return statisticsMapper.getSurveyNum(deptId);
    }

    @Override
    public Integer getCompleteNum(Long deptId) {
        return statisticsMapper.getCompleteNum(deptId);
    }

    @Override
    public BigDecimal getStoreBalance(Long deptId) {
        BigDecimal storeBalance = statisticsMapper.getStoreBalance(deptId);
        if (storeBalance==null){
            storeBalance = BigDecimal.valueOf(0);
        }
        return storeBalance;
    }

    @Override
    public List<Publish> getAnnouncement(Long deptId) {
        return statisticsMapper.getAnnouncement(deptId);
    }

    @Override
    public List<Event> getEventAnnouncement(Long deptId) {
        return statisticsMapper.getEventAnnouncement(deptId);
    }

    @Override
    public Integer pendingAuthNumAdd(Long deptId) {
        return statisticsMapper.pendingAuthNumAdd(deptId);
    }

    @Override
    public Integer surveyNumAdd(Long deptId) {
        return statisticsMapper.surveyNumAdd(deptId);
    }

    @Override
    public Integer completeNumAdd(Long deptId) {
        return statisticsMapper.completeNumAdd(deptId);
    }

    @Override
    public BigDecimal getStoreBalanceChange(Long deptId) {
        Long storeId = statisticsMapper.selectStoreId(deptId);
        BigDecimal lastMonth = statisticsMapper.getLastMonthBalance(storeId);
        BigDecimal thisMonth = statisticsMapper.getThisMonthBalance(storeId);
        if (lastMonth!=null&&thisMonth!=null){
            if (lastMonth.equals(BigDecimal.ZERO)){
                return BigDecimal.valueOf(1);
            }else {
                return (thisMonth.subtract(lastMonth)).divide(lastMonth, RoundingMode.HALF_UP);
            }
        }
        if (thisMonth==null&&lastMonth!=null){
            if (lastMonth.equals(BigDecimal.ZERO)){
                return BigDecimal.valueOf(0);
            }else {
                return BigDecimal.valueOf(-1);

            }
        }
        if (lastMonth==null&&thisMonth!=null){
            if (thisMonth.equals(BigDecimal.ZERO)){
                return BigDecimal.valueOf(0);
            }else {
                return BigDecimal.valueOf(1);
            }
        }
        return BigDecimal.valueOf(0);
    }
}
